package com.huizhonglm.diap.core.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.image.Thumb;


/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2015-08-19 15:05
 */
public class DialogUtil {

    public static void showNormalDialog(Context context, String title, String text, final String left, final String right, final OnDialogBtnClickListener lis){
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_normal);
        TextView leftBtn = (TextView) window.findViewById(R.id.left_btn);// 第一个按钮
        TextView rightBtn = (TextView) window.findViewById(R.id.right_btn);// 第二个按钮
        TextView titleTxt = (TextView) window.findViewById(R.id.title);// 标题
        TextView msgTxt = (TextView) window.findViewById(R.id.message);// 内容
        msgTxt.setText(text);
        if (TextUtils.isEmpty(title)) {
            titleTxt.setVisibility(View.GONE);
        } else {
            titleTxt.setVisibility(View.VISIBLE);
            titleTxt.setText(title);
        }
        leftBtn.setText(left);
        rightBtn.setText(right);
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (lis != null) {
                    lis.onBtnClick(left);
                }
            }
        });
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (lis != null) {
                    lis.onBtnClick(right);
                }
            }
        });
    }

    public static void showConfirmDialog(Context context, String title, String text, final OnDialogBtnClickListener lis){
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_confirm);
        TextView titleTxt = (TextView) window.findViewById(R.id.title);
        TextView msgTxt = (TextView) window.findViewById(R.id.message);
        Button confirmBtn = (Button) window.findViewById(R.id.confirm_btn);

        msgTxt.setText(text);
        if (TextUtils.isEmpty(title)) {
            titleTxt.setVisibility(View.GONE);
        } else {
            titleTxt.setVisibility(View.VISIBLE);
            titleTxt.setText(title);
        }
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (lis != null) {
                    lis.onBtnClick(null);
                }
            }
        });
    }

    public static void showInputDialog(Context context, String title, final String hint, final String defVal, final OnDialogBtnClickListener lis){
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_input);
        TextView leftBtn = (TextView) window.findViewById(R.id.left_btn);// 第一个按钮
        TextView rightBtn = (TextView) window.findViewById(R.id.right_btn);// 第二个按钮
        final EditText inputEdt = (EditText) window.findViewById(R.id.input_edt);// 输入框
        TextView titleTxt = (TextView) window.findViewById(R.id.title);
        titleTxt.setText(title);
        inputEdt.setHint(hint);
        inputEdt.setText(defVal);
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(inputEdt.getText())) {
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    if (lis != null) {
                        lis.onBtnClick(inputEdt.getText().toString());
                    }
                } else {
                    ToastUtil.makeTextShort(hint);
                }
            }
        });
    }

    public static void showSexDialog(Context context, String defChecked, final OnSexCheckedChangedListener lis){
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_sex);
        RadioGroup rgp = (RadioGroup) window.findViewById(R.id.sex_rgp);
        if ("M".equals(defChecked)) {
            rgp.check(R.id.sex_boy_rbtn);
        } else {
            rgp.check(R.id.sex_girl_rbtn);
        }
        rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                dialog.dismiss();
                switch (checkedId) {
                    case R.id.sex_boy_rbtn:
                        lis.onSexCheckedChanged("M");
                        break;
                    case R.id.sex_girl_rbtn:
                        lis.onSexCheckedChanged("F");
                        break;
                }
            }
        });
    }

    public static void showTwoCodeDialog(Context context,String strBarCode){
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_two_code);
        ImageView twoCodeImg = (ImageView) window.findViewById(R.id.two_code_img);
        Thumb.getInstance().display(twoCodeImg, strBarCode, R.drawable.ic_rcode);
    }

    public interface OnDialogBtnClickListener {
        void onBtnClick(String btnText);
    }

    public interface OnSexCheckedChangedListener {
        void onSexCheckedChanged(String sex);
    }

}
